package com.example.admindatasource.transition;

import com.example.admindatasource.mapper.OrderMapper;
import com.example.admindatasource.model.po.OrderLog;
import com.example.adminframework.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Random;
import java.util.UUID;


@Order(3)
@Component
public class OrderTranstion implements ApplicationRunner {
    private final static Logger log = LoggerFactory.getLogger(OrderTranstion.class);

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("自动贩卖机服务启动~");
//        while (1==2){
//            try {
//                addOrder();
//            }catch (Exception exception){
//                log.error("购买失败");
//                log.error(exception.getMessage());
//            }finally {
//                Thread.sleep(300000);
//            }
//        }
    }

    private void addOrder(){
        log.info("用户开始下单");
        OrderLog order = new OrderLog();
        Long userId = new Random().nextLong();
        order.setTime(System.currentTimeMillis());
        order.setUserId(userId);
        order.setOrderMoney(new BigDecimal(new Random(100).nextDouble()));
        order.setOrderCode(
                DateUtil.format(new Date(),DateUtil.YYYYMMDDHHMM)+
                UUID.randomUUID().toString()+
                userId
        );
        orderMapper.insert(order);
        log.info("用户购买成功详情为："+order.toString());
    }

}
